package datastructure.tree;

/**
 *
 * @author Lei
 */
public class GetBalancedBSTFromSortedArray {

    public static TreeNode getBalancedBSTFromSortedArray(int[] arr, int start, int end) {
        if (start > end) {
            return null;
        }

        int middle = (start + end) / 2;
        TreeNode node = new TreeNode(arr[middle]);

        node.left = getBalancedBSTFromSortedArray(arr, start, middle - 1);
        node.right = getBalancedBSTFromSortedArray(arr, middle + 1, end);

        return node;
    }
    
    public static void main(String[] args) {
        
        int[] arr = { 1,2,3,4,5,6,7,8,};
        TreeNode p = GetBalancedBSTFromSortedArray.getBalancedBSTFromSortedArray(arr, 0, arr.length - 1);
        
        System.out.println(p.value);
        System.out.println(p.left);
        System.out.println(p.right);
        //IterativeInorderTravel.method1(p);
        
        
    }
    
}
